package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatueCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import sun.plugin.dom.core.Element;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation("注册功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("开始处理【用户注册】的请求，参数：{}",userRegParam);
        // 将 UserRegParam 转换为 User 实体
        String username = userRegParam.getUsername();

        // 调用 UserMapper 插入用户信息
        int rows = userMapper.selectByUsername(username);
        if (rows > 0) {
            return new JsonResult(StatueCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("即将插入用户信息：{}", user);
        userMapper.insertUser(user);

        return JsonResult.ok();
    }

    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam,@ApiIgnore HttpSession session){
        log.debug("开始处理【用户登录】的请求，参数：{}",userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsernameAndPassword(username,password);
        if (userVO == null) {
            return new JsonResult(StatueCode.USERBANE_PASSWORD_ERROR);
        }
        session.setAttribute("user",userVO);
        return JsonResult.ok(userVO);
    }

    @GetMapping("currentUser")
    @ApiOperation("获取当前登录用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        log.debug("开始处理【获取当前登录用户信息】的请求");
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatueCode.NOT_LOGIN);
        }
        return JsonResult.ok(userVO);
    }

    @GetMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    public JsonResult logout(@ApiIgnore HttpSession session){
        log.debug("开始处理【退出登录】的请求");
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
